<template>
  <div>
    <el-card>
      <el-row type="flex">
        <el-col>
          <el-button size="small" type="primary" @click="isShowDialog = true">添加客户</el-button>
        </el-col>
        <el-col>
          <el-row type="flex">
            <el-select v-model="searchOption.agent">
              <el-option
                :value="''"
                :label="'全部经纪'"
              />
              <el-option
                v-for="agent in agents"
                :key="agent.id"
                :value="agent.id"
                :label="agent.name"
              />
            </el-select>
            <el-input v-model="searchOption.name" placeholder="请输入关键字" />
            <el-button @click="search">
              筛选
            </el-button>
          </el-row>
        </el-col>
      </el-row>
      <el-divider />
      <el-table
        :data="clients"
        border
      >
        <el-table-column label="图片">
          <template #default="{row}">
            <img :src="row.image" alt="" style="width:200px;height:200px;object-fit:cover;">
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="name" />
        <el-table-column label="邮箱" prop="email" />
        <el-table-column label="电话" prop="phone" />
        <el-table-column label="经纪" prop="agent.name" />
        <el-table-column label="微信" prop="wechat" />
        <el-table-column label="介绍" prop="desc" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button size="mini" type="primary" @click="updateClient(row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="delClient(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-divider />
      <el-row type="flex" justify="center">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          :page-size="pagination._limit"
          @current-change="currentChange"
        />
      </el-row>
    </el-card>
    <el-dialog :title="`${data.id?'编辑':'添加'}客户`" :visible="isShowDialog" destroy-on-close @close="btnClose">
      <el-form ref="form" :model="data" :rules="rules">
        <el-form-item label-width="68px" label="经纪">
          <el-select v-model="data.agent">
            <el-option
              v-for="agent in agents"
              :key="agent.id"
              :value="agent.id"
              :label="agent.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label-width="68px" label="姓名" prop="name">
          <el-input v-model="data.name" />
        </el-form-item>
        <el-form-item label-width="68px" label="邮箱" prop="email">
          <el-input v-model="data.email" />
        </el-form-item>
        <el-form-item label-width="68px" label="电话" prop="phone">
          <el-input v-model="data.phone" />
        </el-form-item>
        <el-form-item label-width="68px" label="微信" prop="wechat">
          <el-input v-model="data.wechat" />
        </el-form-item>
        <el-form-item label-width="68px" label="描述" prop="desc">
          <el-input v-model="data.desc" :autosize="{ minRows: 4, maxRows: 8}" resize="none" type="textarea" />
        </el-form-item>
        <el-form-item label-width="68px" label="封面">
          <div style="padding-left:40px">
            <ImageUpload ref="upload" />
          </div>
        </el-form-item>

      </el-form>
      <template #footer>
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnClose">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addClient, delClient, getClients, getClientsCount, getClientDetail, updateClient } from '@/api/client'
import { getAgents } from '@/api/agent'
export default {
  data() {
    return {
      searchOption: {
        name: '',
        agent: ''
      },
      searchOptionConfirmed: {
        name: '',
        agent: ''
      },
      clients: [],
      pagination: {
        _limit: 2,
        _start: 0
      },
      agents: [],
      total: 0,
      isShowDialog: false,
      data: {
        name: '',
        desc: '',
        email: '',
        phone: '',
        wechat: '',
        agent: '',
        image: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    searchOption: {
      deep: true,
      immediate: true,
      handler() {
        this.searchOptionConfirmed = {}
        for (const key in this.searchOption) {
          if (this.searchOption[key]) {
            this.searchOptionConfirmed[`${key}_contains`] = this.searchOption[key]
          }
        }
      }
    }
  },
  created() {
    this.getClients()
    this.getAgents()
  },
  methods: {
    async getAgents() {
      this.agents = await getAgents({
        _limit: -1
      })
    },
    async getClients() {
      this.clients = await getClients({
        ...this.pagination,
        ...this.searchOptionConfirmed
      })
      this.total = await getClientsCount({
        ...this.pagination,
        ...this.searchOptionConfirmed
      })
    },
    currentChange(newPage) {
      this.pagination._start = (newPage - 1) * this.pagination._limit
      this.getClients()
    },
    async delClient(id) {

      await delClient(id)
      this.$message.success('删除成功')
      this.getClients()
    },
    async updateClient(id) {
      const res = await getClientDetail(id)
      this.data = {
        ...res,
        agent: res.agent?.id
      }
      this.isShowDialog = true
      this.$nextTick(() => {
        if (this.data.image) {
          this.$refs.upload.fileList = [
            { url: this.data.image }
          ]
        }
      })
    },
    async btnOK() {
      await this.$refs.form.validate()
      this.data.image = this.$refs.upload.fileList[0]?.url
      if (this.data.id) {
        await updateClient(this.data)
      } else {
        await addClient(this.data)
      }
      this.$message.success('操作成功')
      this.isShowDialog = false
      this.getClients()
    },
    btnClose() {
      this.data = {}
      this.isShowDialog = false
    },
    search() {
      this.getClients()
    }
  }
}
</script>

<style>

</style>
